﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Repository.ViewModels;
using FRL.DebtChased.Website;

namespace WebApplication.Finance
{

	/// <summary>
	/// Declaration of ClientCollectorAssignmentDefault
	/// </summary>
	public partial class ClientCollectorAssignmentDefault : System.Web.UI.Page
	{

		/////////////////////////////////////////////////////////////
		// Event handlers

		protected void Page_Load(object sender, EventArgs e)
		{

			// If this is a postback...
			if (this.IsPostBack)
			{

			}
			else
			{

				// Get the handover
				MySession mySession = MySession.BindMySession();
				int? iClientId = mySession.PullHandover(this);
				if (iClientId == null)
				{
#if DEBUG
					iClientId = 13991;
#else
					Response.Redirect("~/Finance/Clients.aspx");
#endif // #if DEBUG
				}

				// Set the control id
				clientCollectorAssignmentCtrl.ClientRecordID = null;

			}

		}

		protected void butRevert_Click(object sender, EventArgs e)
		{

			// Reload the page
			this.Reload();

		}

		protected void butSave_Click(object sender, EventArgs e)
		{

			// Get the user=id
			MySession mySession = MySession.BindMySession();
			int iUserId = mySession.UserID;
#if DEBUG
			if (iUserId == 0)
				iUserId = 1;
#endif // #if DEBUG

			// Update the model
			ClientCollectorAssignmentsModel model = clientCollectorAssignmentCtrl.ContextModel;

			// Declare an entities object
			string strError = null;
			using (DebtChasedEntities ent = new DebtChasedEntities())
			{

				// Save model to store
				strError = model.Set(ent, iUserId);

			}

			// If that worked...
			if (strError == null)
				this.Reload();
			else
				(this.Page.Master as FinanceMaster).ErrorMessage = strError;

		}

	}

}